﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientAppViaWebService.BuildingsLogicServiceRef;

namespace ClientAppViaWebService.Forms
{
    public partial class BuildingEditForm : LocationEditForm
    {
        private BuildingsLogicService lService;
        private readonly Int32 ClassID = 29;

        #region Properties
        
        public String Address
        {
            get
            {
                return txtAddress.Text;
            }
            set { txtAddress.Text = value; }
        }
        public Int32 AmountRoom
        {
            get
            {
                return Int32.Parse(txtAmountRoom.Text);

            }
            set { txtAmountRoom.Text = value.ToString(); }
        }
        public Int32 AmountFloor
        {
            get
            {
                return Int32.Parse(txtAmountFloor.Text);

            }
            set { txtAmountFloor.Text = value.ToString(); }
        }
        public BuildingEditForm()
        {
            InitializeComponent();
            lService = new BuildingsLogicService();
            classTextBox.Text = ClassID.ToString();
        }
        #endregion
       
        protected override void SetFormProperties(Object _object)
        {
            Building o = (_object as Building);
            ID = o.ID;
            EXT_ID = o.EXT_ID;
            ParentId = o.PARENT_ID;
            Class = o.CLASS_ID;
            ObjName = o.NAME;
            Square = o.SQUARE;
            ResponsiblePersonId = o.RESPONSIBLE_PERSON_ID;
            Address = o.ADDRESS;
            AmountFloor = o.FLOOR_AMOUNT;
            AmountRoom = o.ROOM_AMOUNT;
        }

        protected override Object RetrieveFormProperties()
        {
            Building obj = new Building();
            obj.ID = ID;
            obj.EXT_ID = EXT_ID;
            obj.PARENT_ID = ParentId;
            obj.CLASS_ID = Class;
            obj.NAME = ObjName;
            obj.DataBaseName = DataBaseName;
            obj.RESPONSIBLE_PERSON_ID = ResponsiblePersonId;
            obj.SQUARE = Square;
            obj.ROOM_AMOUNT = AmountRoom;
            obj.FLOOR_AMOUNT = AmountFloor;
            obj.ADDRESS = Address;

            return obj;
        }

        protected override void LoadEditFormData(int parentNodeID)
        {
            Building p = lService.SelectBuilding_By_DataBase_And_Id(DataBaseName, parentNodeID);
            SetFormProperties(p);
        }

        protected override void UpdateEntity()
        {
            Building obj = RetrieveFormProperties() as Building;
            lService.UpdateBuilding(obj);
        }

        protected override void InsertEntity()
        {
            Building obj = RetrieveFormProperties() as Building;
            lService.InsertBuilding(obj);
        }
    }
}
